<?php

/**
 * Handles operations rewarding the DB and it's posible interactions
 *
 */
class classDB {

	private
		$host,
		$user,
		$psw,
		$db,
		$mySQLLink;

	public function __construct($host, $user, $psw, $db) {
		try {
			$this->mySQLLink = mysql_connect($host, $user, $psw, true);
			mysql_select_db($db, $this->mySQLLink);
			$this->host	= $host;
			$this->user	= $user;
			$this->psw	= $psw;
			$this->db	= $db;
		}
		catch(Exception $e) {
			echo "Exceptio: ".$e->getMessage();
		}
		if(!$this->mySQLLink)
			echo "could not connect to the server. Unknown error.\n";
		/*else
			echo "Connection succesfull.\n";*/

		return;
	}

	public function __destruct() {
		mysql_close($this->mySQLLink);
		unset($this->mySQLLink);
		return;
	}

   /**
	 * Executes a query and returns the resuslting array on succes or false on failure
	 *
	 * @param String	Querystring
	 * @param Const 	mode(MYSQL_ASSOC|MYSQL_NUM|MYSQL_BOTH|MYSQL_OBJECT)
	 * @return Dataset
	 */
	public function query($query, $mode) {
		$result = mysql_query($query, $this->mySQLLink);
		if(!mysql_errno($this->mySQLLink)) {
			if(mysql_num_rows($result) > 0) {
				$arrOut = array();
				if($mode == MYSQL_OBJECT) {
					while($arrRow = mysql_fetch_object($result))
						$arrOut[]=$arrRow;
				}
				else {
					while($arrRow = mysql_fetch_array($result, $mode))
						$arrOut[]=$arrRow;
				}
				return $arrOut;
			}
			else
				return false;
		}
		else {
			echo mysql_error($this->mySQLLink).": ".$query;
			return false;
		}
	}

   public function beginTransaction() {
		@mysql_query("BEGIN", $this->mySQLLink);
		return true;
	}

	public function commitransaction() {
		@mysql_query("COMMIT", $this->mySQLLink);
		return true;
	}

	public function rollBackTransaction() {
		@mysql_query("ROLLBACK", $this->mySQLLink);
		return true;
	}

#SPENT METHODS###########################################################
	/**
	* Inserts a new row in table 'spent'
	*
	* @param mixed $intCategory
	* @param mixed $strDate
	* @param mixed $flValue
	* @param mixed $strDescription
	* @return int
	*/
	public function insertSpent($intCategory, $strDate, $flValue, $intPayments = 1,$strDescription) {

   $strQuery = "
			INSERT IGNORE INTO
				spent
			SET
				category_id	= $intCategory,
				date			= '$strDate',
				value			= $flValue,
				description	= '$strDescription',
				payments		= $intPayments"
		;
		mysql_query($strQuery, $this->mySQLLink);
		if(mysql_errno($this->mySQLLink)) {
			echo mysql_error($this->mySQLLink)."\n";
			return false;
		}
		else
			return mysql_affected_rows($this->mySQLLink);
}
#########################################################################

#CATEGORY METHODS########################################################
	/**
	* Find the category for the given ID.
	*
	* @param Int Category ID
	*
	* @return Dataset|Bool
	*/
	public function getCategoryById($intID) {

		$strQuery = "
			SELECT
				id,
				name,
				description
			FROM
				category
			WHERE
				id = $intID"
		;
	   $result = mysql_query($strQuery, $this->mySQLLink);

	   if(!mysql_errno($this->mySQLLink)) {
			if(mysql_num_rows($result) > 0)
				return mysql_fetch_object($result);
			else
				return false;
		}
		else {
			echo mysql_error($this->mySQLLink).": ".$query;
			return false;
		}
	}

	/**
	* Find the category for the given name.
	*
	* @param String Category name
	*
	* @return Dataset|Bool
	*/
	public function getCategoryByName($strName) {

		$strQuery = "
			SELECT
				id,
				name,
				description
			FROM
				category
			WHERE
				name = '".strtolower($strName)."'"
		;
	   $result = mysql_query($strQuery, $this->mySQLLink);

	   if(!mysql_errno($this->mySQLLink)) {
			if(mysql_num_rows($result) > 0)
				return mysql_fetch_object($result);
			else
				return false;
		}
		else {
			echo mysql_error($this->mySQLLink).": ".$query;
			return false;
		}
	}
#########################################################################
	/**
	* Checks if the given date range in a valid one
	*
	* @param String Start date
	* @param String End date
	*
	* @return Bool
	*/
	private function checkDateRange($strStartDate, $strEndDate) {

		$arrStartDate	= explode("/", $strStartDate);
		$arrEndDate		= explode("/", $strEndDate);

		if(!checkdate($arrStartDate[1], $arrStartDate[0], $arrStartDate[2])) {
			echo "Wrong date value for strStartDate: $strStartDate";
			return false;
		}

   	if(isset($strEndDate)) {
         if(!checkdate($arrStartDate[1], $arrStartDate[0], $arrStartDate[2])) {
				echo "Wrong date value for strEndtDate: $strEndDate";
				return false;
			}
		}

		$intStartDate	= mktime(0, 0, 0,	$arrStartDate[1],	$arrStartDate[0],	$arrStartDate[2]);
      $intEndDate		= mktime(0, 0, 0,	$arrEndDate[1],	$arrEndDate[0],	$arrEndDate[2]);
      if($intEndDate <= $intStartDate) {
			echo "This is not a valid date range: $strStartDate - $strEndDate";
			return false;
      }
      else
      	return true;
	}
}
?>
